package com.example.tt_bells_v02;

//import java.io.Serializable;
import java.util.List;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterBellTemplate extends BaseAdapter implements OnLongClickListener {

	static final int KEY_POSITION = 1;
	//ServiceConnection sconn;
	//boolean bound = false;
	
	List<BellTemplate> templateHolder = null;
	
	public AdapterBellTemplate(List<BellTemplate> datalink) {
		super();
		
		templateHolder = datalink;
	}

	@Override
	public int getCount() {
		return templateHolder.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	private final class MyTouchListener implements OnTouchListener {
	    public boolean onTouch(View view, MotionEvent motionEvent) {
	    	int pos = Integer.parseInt(view.getTag().toString());
	    	if (pos == 0) return false; //do not move "add new"
	    	
	      if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
	        ClipData data = ClipData.newPlainText("", "");
	        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
	        view.startDrag(data, shadowBuilder, view, 0);
	        //view.setVisibility(View.INVISIBLE);
	        return true;
	      } else {
	        return false;
	      }
	    }
	  }
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Context context = parent.getContext();
		BellTemplate selected = templateHolder.get(position);
		
		View retval = LayoutInflater.from(context).inflate(R.layout.list_item_template, null);
		TextView title = (TextView) retval.findViewById(R.id.title);
		ImageView iv = (ImageView) retval.findViewById(R.id.image);
		if (position == 0) {
			iv.setImageDrawable(context.getResources().getDrawable(R.drawable.plus));
		}
		//iv.setOnClickListener(mOnButtonClicked);
		iv.setOnLongClickListener(this);
		iv.setOnTouchListener(new MyTouchListener());
		iv.setTag(position);
		//Button button = (Button) retval.findViewById(R.id.clickbutton);
		//button.setOnClickListener(mOnButtonClicked);
		title.setText(selected.name_short);
		
		return retval;
	}
	
	@Override
	public boolean onLongClick(View v) {
		int pos = Integer.parseInt(v.getTag().toString());
		Log.d("get Tag: ", " position = "+v.getTag());
		Intent aaa = new Intent(v.getContext(),ActivityEditBellInfo.class);
		//BellTemplate bt = (BellTemplate)templateHolder.get(pos);
		//aaa.putExtra("template", (BellTemplate)templateHolder.get(pos));
		aaa.putExtra("index",pos);
		aaa.putExtra("type", BellInfo.EDIT_TEMPLATE);
		v.getContext().startActivity(aaa);
		
		return false;
	}
	
}
